var classservice = require("../../../apiservice/class/classservice");
var schoolservice = require("../../../apiservice/school/schoolservice");
var type = "detail";
const jsonData = require("../../tasks/detail/jsondata");
const userInfo = getApp().globalData.userInfo;

Page({
	data: {
		stage: 0,
		editShow: false,
		userinfo: [],
		region: ['湖南省', '长沙市', '芙蓉区'],
		schoolType: jsonData.schoolType, //学段选项集
		schoolTypeIndex: 0, //学段索引
		schoolIndex: 0, //学校索引
		index: 0,
		classlist: [],
		classinfo: [],
		schoolList: [{
			new_name: '全部',
			new_schoolid: null
		}], //学校选项集
		calssname: '',
		stage: 0,
		schoolId: '',
		customItem: '全部',
		userid: '',
		showDistrict: false,
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		if (userInfo) {
			this.setData({
				userid: userInfo.userid
			});
			var region = [];
			region.push(userInfo.new_provincename);
			region.push(userInfo.new_cityname);
			if (userInfo.new_areaname) {
				region.push(userInfo.new_areaname);
			} else {
				region.push('');
			}
			this.setData({
				region: region
			})
		}
		if (options && options.stage) {
			this.setData({
				stage: options.stage
			})
		}
		if (options && options.schoolId) {
			this.setData({
				schoolId: options.schoolId
			})
			type = options.type;
			//用于解决当这家学校没有班级的时候，学校显示出错问题
			// this.getSchoolDetail(options.schoolId);
			// this.getClassBySchoolIdAndClassName(options.schoolId);
		} else {
			type = "detail";
			this.QueryClassByPCAandStageSchool(this.data.region[0], this.data.region[1], this.data.region[2], this.data.stage,
				this.data.schoolid)
			var schoolObj = {
				new_provinceName: this.data.region[0],
				new_cityName: this.data.region[1],
				new_areaName: this.data.region[2]
			};
			this.getSchoolListByType(schoolObj);
			this.setData({
				userinfo: userInfo,
				schoolid: null
			});
		}

	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		if (this.data.schoolId) {
			//wx.showLoading();
			this.getSchoolDetail(this.data.schoolId);

		} else {
			this.QueryClassByPCAandStageSchool(this.data.region[0], this.data.region[1], this.data.region[2], this.data.stage,
				this.data.schoolid)
		}
	},
	onSearch: function(e) {
		//console.log("用户搜索"+e.detail.searchKey);

		if (e.detail.searchKey.replace(/\s+/g, '') == '') {
			wx.showToast({
				title: '请输入班级名称',
				icon: 'none',
				duration: 2000
			});
		} else {
			wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
				title: '加载中',
				icon: 'loading'
			});
			this.getClassByPACAndName(this.data.region[0], this.data.region[1], this.data.region[2], e.detail.searchKey, this
				.data.stage, this.data.schoolid);
			wx.hideLoading();
		}

	},
	editItem: function(e) {
		this.getClassById(e.currentTarget.dataset.id);
		this.setData({
			editShow: true
		})
	},
	enterDetail: function(e) {
		var currentTarget = e.currentTarget;
		if (type == "choose") {
			var pages = getCurrentPages();
			var prevPage = pages[pages.length - 2]; //上一个页面
			//直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
			//debugger;
			prevPage.setData({
				["screenSchoolItem.classNum"]: currentTarget.dataset.classnum,
				["screenSchoolItem.className"]: currentTarget.dataset.name,
				["screenSchoolItem.classId"]: currentTarget.id
			});
			wx.navigateBack({
				delta: 1
			})
		} else {
			wx.navigateTo({
				url: '/pages/class/detail/detail?id=' + e.currentTarget.id
			})
		}
	},
	districtTap: function(e) {
		this.setData({
			showDistrict: true
		});
	},
	bindSchoolChange: function(e) {
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
			title: '加载中',
			icon: 'loading'
		});
		this.setData({
			schoolIndex: e.detail.value
		});
		if (e.detail.value == "0") {
			console.log(this.data.stage);
			this.QueryClassByPCAandStageSchool(this.data.region[0], this.data.region[1], this.data.region[2], this.data.stage,
				null);
		} else {
			this.setData({
				schoolid: this.data.schoolList[e.detail.value].new_schoolId
			});
			this.QueryClassByPCAandStageSchool(this.data.region[0], this.data.region[1], this.data.region[2], this.data.stage,
				this.data.schoolid);
		}
		wx.hideLoading();
	},
	//学校类型
	bindSchoolTypeChange: function(e) {
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
			title: '加载中',
			icon: 'loading'
		});
		this.setData({
			schoolTypeIndex: e.detail.value,
			schoolList: [{
				new_name: '全部',
				new_schoolid: null
			}],
			schoolIndex: 0,
			stage: e.detail.value,
			schoolid: null
		});
		if (e.detail.value == "0") {
			this.getClassByPCA(this.data.region[0], this.data.region[1], this.data.region[2]);
			var schoolObj = {
				new_provinceName: this.data.region[0],
				new_cityName: this.data.region[1],
				new_areaName: this.data.region[2]
			};
			this.getSchoolListByType(schoolObj);
		} else {
			var schoolObj = {
				new_provinceName: this.data.region[0],
				new_cityName: this.data.region[1],
				new_areaName: this.data.region[2],
				new_stage: e.detail.value
			};
			this.getSchoolListByType(schoolObj);
			this.QueryClassByPCAandStageSchool(this.data.region[0], this.data.region[1], this.data.region[2], e.detail.value,
				null);
		}
		wx.hideLoading();
	},

	addClass: function() {
		// var provinceid= this.data.classlist[0].new_province;
		// var cityid= this.data.classlist[0].new_city;
		// var countyid= this.data.classlist[0].new_area;
		var schoolid = this.data.schoolId;
		var schoolIndex = this.data.schoolIndex;
		var schoolname = '';
		if (schoolIndex != '-1') {
			schoolname = this.data.schoolList[schoolIndex].new_name;
		}
		wx.navigateTo({
			url: `/pages/class/add/add?schoolname=${schoolname}&schoolid=${schoolid}` //+'?provinceid='+provinceid+'&cityid='+cityid+'&countyid='+countyid
		})

	},
	// 位置监听事件
	bindRegionChange: function(e) {

		this.setData({
			showDistrict: false
		});
		var provinceName = e.detail.currentTarget.dataset.province;
		var cityName = e.detail.currentTarget.dataset.city;
		var areaName = e.detail.currentTarget.dataset.area;

		var region = [];
		region.push(provinceName);
		region.push(cityName);
		region.push(areaName);
		if (provinceName != '全部') {
			this.setData({
				region: region,
				schoolList: [{
					new_name: '全部',
					new_schoolid: null
				}],
				schoolIndex: 0,
				schoolTypeIndex: 0, //学段索引
			})
			this.getClassByPCA(region[0], region[1], region[2]);

			var schoolObj = {
				new_provinceName: region[0],
				new_cityName: region[1],
				new_areaName: region[2]
			};
			this.getSchoolListByType(schoolObj);
			wx.hideLoading();
		} else {
			wx.showToast({
				title: "省区不能选全部",
				icon: 'none',
				mask: true,
				duration: 2000
			});
			return;
		}
	},
	getClassByPCA: function(province, city, area) {
		var that = this;
		classservice.getClassByPAC(province, city, area).then(function(success) {
			console.log(success);
			that.setData({
				classlist: success.data
			});
		}).catch(function(ex) {
			console.log(ex);
		})
	},

	//根据其他页面传过来学校ID查询班级
	getClassBySchoolIdAndClassName: function(schoolId, region, classname = "") {
		let that = this;
		classservice.QueryClassBySchoolIdAndClassName(schoolId, classname).then(function(success) {
			let result = success.data;
			let region = that.data.region;
			let schoolObj;
			let stage = that.data.stage;
			let provinceName = region[0];
			let cityName = region[1];
			let areaName = region[2];
			if (!result || result.length == 0) {
				schoolObj = {
					new_provinceName: provinceName,
					new_cityName: cityName,
					new_areaName: areaName,
					new_stage: stage
				};
			} else {
				provinceName = result[0].new_provinceName;
				cityName = result[0].new_cityName;
				areaName = result[0].new_areaName;
				stage = result[0].new_stage;
				schoolObj = {
					new_provinceName: provinceName,
					new_cityName: cityName,
					new_areaName: areaName,
					new_stage: stage
				};
			}
			//debugger;
			that.getSchoolListByType(schoolObj, function(schoolList) {
				var schoolIndex = schoolList.findIndex((value, index, arr) => {
					return value.new_schoolId === schoolId
				});
				//debugger;
				that.setData({
					schoolIndex: schoolIndex
				});
			});
			that.setData({
				classlist: result,
				region: [provinceName, cityName, areaName],
				schoolTypeIndex: stage,
				schoolid: schoolId
			});
			//wx.hideLoading();
		}).catch(function(ex) {
			//wx.hideLoading();
			console.log(ex);
		})
	},

	/**
	 * @param {*} province 省名称
	 * @param {*} city 市名称
	 * @param {*} area 区名称
	 * @param {*} name 班级名称
	 * @param {*} stage 学段
	 * @param {*} schoolid 学校ID
	 */
	getClassByPACAndName: function(province, city, area, name, stage, schoolid) {
		var that = this;
		classservice.getClassByPACAndName(province, city, area, name, stage, schoolid).then(function(success) {
			console.log(success);
			that.setData({
				classlist: success.data
			});
		}).catch(function(ex) {
			console.log(ex);
		})
	},
	onChange: function(e) {
		var att = 'classinfo.new_classnumber';
		this.setData({
			[att]: e.detail
		})
	},
	// 修改班级信息
	onSure: function(e) {
		var newname = this.data.classinfo.new_schoolName + this.data.classinfo.new_classnumber
		var classobj = {
			new_classId: this.data.classinfo.new_classId,
			new_classnumber: this.data.classinfo.new_classnumber,
			new_name: newname,
			userid: this.data.userinfo.userid
		};
		console.log(classobj);
		this.modClassById(classobj);
	},
	onClose: function() {
		this.setData({
			editShow: false
		})
	}, // 根据班级ID获取班级信息
	getClassById: function(id) {
		var that = this;
		classservice.getClassById(id).then(function(success) {
			console.log(success);
			that.setData({
				classinfo: success.data
			});
		}).catch(function(ex) {
			console.log(ex);
		})
	},
	// 修改班级信息
	modClassById: function(obj) {
		var that = this;
		classservice.modClassById(obj).then(function(success) {
			console.log(success);
			that.setData({
				editShow: false
			});
			that.showResult(success.data);
			//that.onLoad();
			that.onShow();
		}).catch(function(ex) {
			that.setData({
				editShow: false
			});
			console.log(ex);
		})
	},
	getSchoolListByType: function(obj, funName) {
		var that = this;
		classservice.getSchoolListByType(obj).then(function(success) {
			var list = that.data.schoolList.concat(success.data);
			that.setData({
				schoolList: list
			});
			if (typeof funName === "function") {
				funName(that.data.schoolList);
			}
		}).catch(function(ex) {
			console.log(ex);
		})
	},
	QueryClassByPCAandStageSchool: function(provinceName, cityName, areaName, stage, schoolid) {
		var that = this;
		classservice.QueryClassByPCAandStageSchool(provinceName, cityName, areaName, stage, schoolid).then(function(
			success) {
			console.log(success.data);
			//var list=that.data.schoolList.concat(success.data);
			//console.log(list);
			that.setData({
				classlist: success.data
			});
		}).catch(function(ex) {
			console.log(ex);
		})

	},
	showResult: function(res) {
		var title = '';
		var icon = '';
		if (res.resultState == 1) {
			title = '修改成功';
			icon = 'success';
		} else {
			title = '修改失败';
			icon = 'none';
		}
		wx.showToast({
			title: title,
			icon: icon,
			duration: 2000
		});
	},
	getSchoolDetail: function(schoolId) {
		//debugger;
		var that = this;
		schoolservice.getSchoolDetail(schoolId).then(function(success) {
			console.log(success);
			var result = success.data;
			var region = [];
			region.push(result.new_provinceName);
			region.push(result.new_cityName)
			region.push(result.new_areaName)
			that.setData({
				region: region
			})
			that.getClassBySchoolIdAndClassName(schoolId);
		}).catch(function(ex) {
			console.log(ex);
		});
	}
})
